package com.ruoyi.web.pojo.dto.screen;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.web.domain.screen.BigScreen;
import com.ruoyi.web.pojo.request.screen.BigScreenExten;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.Objects;

/**
 * 大屏配置对象 tt_big_screen
 * 
 * @author zq
 * @date 2024-01-15
 */
@Data
@NoArgsConstructor
public class BigScreenRep extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** $column.columnComment */
    private Long id;

    /** 类型 */
    @Excel(name = "类型")
    private String type;

    /** 名称 */
    @Excel(name = "名称")
    private String name;

    /** 影视 */
    @Excel(name = "影视")
    private String movies;

    /** 状态（0 上架、1下架） */
    @Excel(name = "状态", readConverterExp = "0=,上=架、1下架")
    private String state;

    /** 温度 */
    @Excel(name = "温度")
    private String temp;

    /** 湿度 */
    @Excel(name = "湿度")
    private String humidity;

    /** 氮 */
    @Excel(name = "氮")
    private String nitrogen;

    /** 磷 */
    @Excel(name = "磷")
    private String phosphorus;

    /** 扩展 */
    @Excel(name = "扩展")
    private BigScreenExten extensions;

    /** 方案 */
    @Excel(name = "方案")
    private String program;

    @ApiModelProperty("开始时间 yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date startTime;
    @ApiModelProperty("结束时间 yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date endTime;


    public static BigScreenRep toBigScreenRep(BigScreen bigScreen) {
        if (bigScreen == null) {
            return null;
        }
        BigScreenRep bigScreenRep = new BigScreenRep();
        bigScreenRep.setId(bigScreen.getId());
        bigScreenRep.setType(bigScreen.getType());
        bigScreenRep.setName(bigScreen.getName());
        bigScreenRep.setMovies(bigScreen.getMovies());
        bigScreenRep.setState(bigScreen.getState());
        bigScreenRep.setTemp(bigScreen.getTemp());
        bigScreenRep.setHumidity(bigScreen.getHumidity());
        bigScreenRep.setNitrogen(bigScreen.getNitrogen());
        bigScreenRep.setPhosphorus(bigScreen.getPhosphorus());
        bigScreenRep.setProgram(bigScreen.getProgram());
        bigScreenRep.setStartTime(bigScreen.getStartTime());
        bigScreenRep.setEndTime(bigScreen.getEndTime());

        if (Objects.nonNull(bigScreen.getExtensions())){
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
                BigScreenExten s = objectMapper.readValue(bigScreen.getExtensions(), BigScreenExten.class);
                bigScreenRep.setExtensions(s);
            }catch (Exception e){
                throw new  RuntimeException("json转换异常");
            }
        }
        return bigScreenRep;
    }


    public  BigScreenRep (BigScreen bigScreen) {

        this.setId(bigScreen.getId());
        this.setType(bigScreen.getType());
        this.setName(bigScreen.getName());
        this.setMovies(bigScreen.getMovies());
        this.setState(bigScreen.getState());
        this.setTemp(bigScreen.getTemp());
        this.setHumidity(bigScreen.getHumidity());
        this.setNitrogen(bigScreen.getNitrogen());
        this.setPhosphorus(bigScreen.getPhosphorus());
        this.setProgram(bigScreen.getProgram());
        this.setStartTime(bigScreen.getStartTime());
        this.setEndTime(bigScreen.getEndTime());

        if (Objects.nonNull(bigScreen.getExtensions())){
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                BigScreenExten s = objectMapper.readValue(bigScreen.getExtensions(), BigScreenExten.class);
                this.setExtensions(s);
            }catch (Exception e){
                throw new  RuntimeException("json转换异常");
            }
        }
    }
}
